package com.df.happylearn.request;

import com.android.volley.Cache;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.df.happylearn.model.ArticleContentInfo;
import com.df.happylearn.model.ArticleInfo;
import com.df.happylearn.model.MessageInfo;
import com.df.happylearn.utils.CacheUtil;
import com.df.happylearn.utils.Constants;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class DataProvider {

    private static class SingletonHolder {
        private static final DataProvider INSTANCE = new DataProvider();
    }

    public static final DataProvider getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private DataProvider() {

    }

    public interface Callback<T> {
        void onSuccess(T data);

        void onError(VolleyError error);
    }

    public void getArticleList(int cat_id, int page, int size, final Callback<ArrayList<ArticleInfo>> callback) {
        String url = Constants.URL_ARTICLE_LIST + "?cat_id=" + cat_id + "&page=" + page + "&size=" + size;
        parseArticleList(url, callback);
    }

    public void parseArticleList(String url, final Callback<ArrayList<ArticleInfo>> callback) {
        String tag_json_obj = "json_req_getCourseList" + url;
        Utf8JsonRequest request = new Utf8JsonRequest(url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(final JSONObject response) {
                        try {
                            int status = response.getInt("status");
                            if (status > 0) {
                                JSONArray data = response.getJSONArray("data");
                                ArrayList<ArticleInfo> dataList = ArticleInfo.parseJsonArray(data);
                                callback.onSuccess(dataList);
                            } else {
                                callback.onSuccess(new ArrayList<ArticleInfo>());
                            }
                        } catch (Exception e) {
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onError(error);
                    }
                });
        request.setShouldCache(false);
        DataLoader.getInstance().addToRequestQueue(request, tag_json_obj);
    }

    public void getMessage(int uid, int page, int size, String utime, final Callback<ArrayList<MessageInfo>> callback, boolean clearCache) {
        String url = Constants.URL_MESSAGE_LIST + "?uid=" + uid + "&page=" + page + "&size=" + size + "&utime=" + utime;
        if (clearCache) {
            Cache cache = DataLoader.getInstance().getRequestQueue().getCache();
            cache.remove(url);
        }
        parseMessageList(url, callback);
    }

    public void parseMessageList(final String url, final Callback<ArrayList<MessageInfo>> callback) {
        String tag_json_obj = "json_req_getCourseList" + url;
        Utf8JsonRequest request = new Utf8JsonRequest(url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(final JSONObject response) {
                        try {
                            int status = response.getInt("status");
                            if (status > 0 && !response.isNull("data")) {
                                JSONArray data = response.getJSONArray("data");
                                ArrayList<MessageInfo> dataList = MessageInfo.parseJsonArray(data);
                                callback.onSuccess(dataList);
                            } else {
                                callback.onSuccess(new ArrayList<MessageInfo>());
                            }
                        } catch (Exception e) {
                        }
                        // 缓存
                        CacheUtil.getInstance().getMessageCache().put(url, response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 提取缓存
                        JSONObject response = CacheUtil.getInstance().getMessageCache().getAsJSONObject(url);
                        if (response != null) {
                            try {
                                int status = response.getInt("status");
                                String utime = response.getString("utime");
                                if (status > 0 && !response.isNull("data")) {
                                    JSONArray data = response.getJSONArray("data");
                                    ArrayList<MessageInfo> dataList = MessageInfo.parseJsonArray(data);
                                    callback.onSuccess(dataList);
                                } else {
                                    callback.onSuccess(new ArrayList<MessageInfo>());
                                }
                            } catch (Exception e) {
                            }
                        } else {
                            callback.onError(error);
                        }
                    }
                });
        request.setShouldCache(false);
        DataLoader.getInstance().addToRequestQueue(request, tag_json_obj);
    }

    public void getArticleContent(int article_id, final Callback<ArticleContentInfo> callback) {
        String url = Constants.URL_ARTICLE_CONTENT + "?article_id=" + article_id;
        parseArticleContent(url, callback);
    }

    public void parseArticleContent(String url, final Callback<ArticleContentInfo> callback) {
        String tag_json_obj = "json_req_getCourseContent" + url;
        Utf8JsonRequest request = new Utf8JsonRequest(url,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(final JSONObject response) {
                        try {
                            int status = response.getInt("status");
                            if (status > 0) {
                                JSONObject data = response.getJSONObject("data");
                                ArticleContentInfo bean = ArticleContentInfo.parseJson(data);
                                callback.onSuccess(bean);
                            } else {
                                callback.onError(new VolleyError("request error!"));
                            }
                        } catch (Exception e) {
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onError(error);
                    }
                });

        request.setShouldCache(false);
        DataLoader.getInstance().addToRequestQueue(request, tag_json_obj);
    }

}
